package weaver.interfaces.cese.duss.action.fee;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-09-24
 * @Description: 费用报销流程归档后插入记录到人员信用积分记录
 * @ModifyLog:
 **/

public class WeaRecordStaffCreditAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        int requestid = requestInfo.getRequestManager().getRequestid();
        int workflowid = requestInfo.getRequestManager().getWorkflowid();
        final int MAX_SCORD = 4;
        writeLog( "----WeaRecordStaffCreditAction-------" + requestid );
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        try {
            rs.executeQuery( "select sqr,fyfsr,djrq from " + tablename + " where requestid=?", requestid );
            rs.next();
            String sqr = Util.null2String( rs.getString( "sqr" ) );//申请人
            String fyfsr = Util.null2String( rs.getString( "fyfsr" ) );//费用承担人
            String djrq = Util.null2String( rs.getString( "djrq" ) );//单据日期
            //发起节点
            if(sqr.equals(fyfsr)){
                int fz = getRecordScord( sqr, djrq );//分值
                if (fz < MAX_SCORD) {
                    String sql = "insert into uf_ryxyjfjl(yg,jfkf,rq,fz,dylc)values(?,?,?,?,?)";
                    rs.executeUpdate( sql, new Object[]{sqr, 0, djrq, 1, requestid} );
                }
            }else {
                int fz1 = getRecordScord( sqr, djrq );//申请人-分值
                int fz2 = getRecordScord( fyfsr, djrq );//费用承担人-分值
                if (fz1 < MAX_SCORD || fz2 < MAX_SCORD) {
                    String sql1 = "insert into uf_ryxyjfjl(yg,jfkf,rq,fz,dylc)values(?,?,?,?,?)";
                    rs1.executeUpdate( sql1, new Object[]{sqr, 0, djrq, 1, requestid} );
                    String sql2 = "insert into uf_ryxyjfjl(yg,jfkf,rq,fz,dylc)values(?,?,?,?,?)";
                    rs1.executeUpdate( sql2, new Object[]{fyfsr, 0, djrq, 1, requestid} );
                }

            }
            //审核节点
            String xgbmsh = getPropValue( "freeCostPro", "xgbmsh" );//相关部门审核
            String xmjlsh = getPropValue( "freeCostPro", "xmjlsh" );//项目经理审核
            String fgssh = getPropValue( "freeCostPro", "fgssh" );//分公司审核
            String znbmsh = getPropValue( "freeCostPro", "znbmsh" );//职能部门审核
            String sql1 = "select operator from  workflow_requestlog where workflowid=" + workflowid + " \n" +
                    "and nodeid in('" + xgbmsh + "','" + xmjlsh + "','" + fgssh + "','" + znbmsh + "') and requestid='" + requestid + "' group by operator";
            rs.executeQuery( sql1 );
            while (rs1.next()) {
                String operator = Util.null2String( rs1.getString( "operator" ) );
                if(getRecordScord(operator,djrq)<MAX_SCORD){
                    String sql2 = "insert into uf_ryxyjfjl(yg,jfkf,rq,fz,dylc)values(?,?,?,?,?)";
                    rs1.executeUpdate( sql2, new Object[]{operator, 0, djrq, 0.1, requestid} );
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    public static int getRecordScord(String ry, String djrq) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select sum(fz) from uf_ryxyjfjl where yg='" + ry + "' and substr(rq,6,2)='" + djrq.substring( 5, 7 ) + "'" );
        rs.next();
        int fz = Util.getIntValue( rs.getString( 1 ) );
        return fz;
    }
}
